package com.example.vv.frameWork.view;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :create by lhw
 * time :{DATE} {HOUR}
 * email :785661432@qq.com
 * phone :13018600506
 * 内容:
 */
abstract  public class MyAdapter<B,T extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<T> {
    private List<B> myData=new ArrayList<>();
    public Context mContext;
    protected LayoutInflater inflater;

    public MyAdapter(Context mContext) {
        this.mContext = mContext;
        inflater=LayoutInflater.from(this.mContext);
    }

    public List<B> getMyData() {
        return myData;
    }

    public void setMyData(List<B> myData) {
        this.myData = myData;
        notifyDataSetChanged();
    }
    public void addMyData(List<B> myData){
        this.myData.addAll(myData);
        notifyDataSetChanged();
    }

    //废弃
    @Deprecated
    public void reflushData(List<B> myData){
        if (this.myData!=null){
            this.myData.clear();
            for (int i = 0; i < myData.size(); i++) {
                this.myData.add(myData.get(i));
            }
            notifyDataSetChanged();
        }else{
            setMyData(myData);
        }
    }

    /**
     * @param viewGroup
     * @param position
     * @return
     */

    @Override
    public T onCreateViewHolder(ViewGroup viewGroup, int position) {
        View inflate=null;

        if(setLayout()!=0){
            //初始化视图
            inflate = inflater.inflate(setLayout(), viewGroup, false);
        }
        return setViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(T t, int position) {
        initView(myData.get(position),position,t);
    }

    @Override
    public int getItemCount() {
        return myData==null?0:myData.size();
    }

    /**
     * 设置布局id
     * @return
     */
    abstract  public int setLayout();

    /**
     * 实例化viewHolder
     * @return
     */
    abstract public T setViewHolder(View view);

    /**
     * 初始化数据
     * @param data
     * @param position
     * @param viewHolder
     */
    protected abstract void initView(B data, int position, T viewHolder);


}
